<?php

namespace app\service;

//微信小店
use app\enum\UserCode;
use app\enum\WxShopCode;
use app\model\User;
use app\model\UserReward;
use library\wx_shop;
use support\Log;

class WxShopService extends Base
{

    //微信小店-奖励生成
    public function wxShopRewardInsert($order_id, $user_id, $invitee_id, $order_info = null)
    {
        $user = User::where('id', $user_id)->find();
        if (!empty($user)) {
            if ($user->is_agent) {
                $data = [
                    'order_id' => $order_id,
                    'user_id' => $user_id,
                    'invitee_id' => $invitee_id,
                    'amount' => 10,
                    'status' => UserCode::REWARD_STATUS['待入账'],
                    'order_status' => UserCode::ORDER_STATUS['已支付待发货'],
                    'order_time' => date('Y-m-d H:i:s'),
                    'order_info' => $order_info
                ];

            } else {
                $data = [
                    'order_id' => $order_id,
                    'user_id' => $user_id,
                    'invitee_id' => $invitee_id,
                    'amount' => 5,
                    'status' => UserCode::REWARD_STATUS['待入账'],
                    'order_status' => UserCode::ORDER_STATUS['已支付待发货'],
                    'order_time' => date('Y-m-d H:i:s'),
                    'order_info' => $order_info
                ];
                $this->checkUserAgent($order_id, $user->agent_id, $invitee_id, $order_info);
            }
            UserReward::create($data);
        }

    }


    //检测用户上级代理
    public function checkUserAgent($order_id, $agent_id, $invitee_id, $order_info = null)
    {
        $user = User::where('id', $agent_id)->find();

        if (!empty($user) && $user->is_agent) {
            //是代理  给他入账
            $data = [
                'order_id' => $order_id,
                'user_id' => $user->id,
                'invitee_id' => $invitee_id,
                'amount' => 5,
                'status' => UserCode::REWARD_STATUS['待入账'],
                'order_status' => UserCode::ORDER_STATUS['已支付待发货'],
                'order_time' => date('Y-m-d H:i:s'),
                'order_info' => $order_info
            ];
            UserReward::create($data);
        }
    }


    public function wxShopRewardUpdate($order_id, $order_status, $order_info = null, $status = false)
    {
        $user_reward_list = UserReward::where('order_id', $order_id)->select();
        foreach ($user_reward_list as $user_reward) {
            if ($user_reward) {
                !empty($status) && $user_reward->status = $status;
                $user_reward->order_status = $order_status;
                $user_reward->order_info = $order_info;
                $user_reward->save();
            }
        }

    }


    //用户订单更新
    public function wxShopOrderUpdate($event, $order_id)
    {
        $order = $this->getWxShopOrder($order_id);
        if (empty($order)) {
            Log::error('从小店获取订单失败');
            return false;
        }
        $order_info = $order['order_info'];

        //查询奖励
        $shop_reward = UserReward::where('order_id', $order_id)->count();


        switch ($event) {
            case 'channels_ec_order_pay':
                //订单支付成功
                if ($order['status'] == WxShopCode::ORDER_STATUS['待发货']) {
                    $buy_user = UserService::get_user_by_unionid($order['unionid']);
                    if ($buy_user) {
                        if (!empty($shop_reward)) {
                            Log::error('订单已生成过奖励');
                            return false;
                        }
                        if ($buy_user->agent_id) {
                            //生成奖励
                            $this->wxShopRewardInsert($order['order_id'], $buy_user->agent_id, $buy_user['id'], $order_info);
                        }
                    }
                }
                break;
            case 'channels_ec_order_deliver':
                //订单发货
                if ($order['status'] == WxShopCode::ORDER_STATUS['待收货']) {
                    //查询unionid
                    $buy_user = UserService::get_user_by_unionid($order['unionid']);
                    if ($buy_user) {
                        if (empty($shop_reward)) {
                            Log::error('订单未生成');
                            return false;
                        }
                        //修改订单状态
                        $this->wxShopRewardUpdate($order['order_id'], UserCode::ORDER_STATUS['已发货待收货'], $order_info);
                    }
                }
                break;
            case 'channels_ec_order_confirm':
                //订单确认收货
                if ($order['status'] == WxShopCode::ORDER_STATUS['完成']) {
                    //查询unionid
                    $buy_user = UserService::get_user_by_unionid($order['unionid']);
                    if ($buy_user) {
                        if (empty($shop_reward)) {
                            Log::error('订单未生成');
                            return false;
                        }
                        //修改订单状态
                        $this->wxShopRewardUpdate($order['order_id'], UserCode::ORDER_STATUS['已收货待入账'], $order_info);
                    }
                }
                break;
            case 'channels_ec_order_settle':
                //订单结算成功
                if ($order['status'] == WxShopCode::ORDER_STATUS['完成']) {
                    //查询unionid
                    $buy_user = UserService::get_user_by_unionid($order['unionid']);
                    if ($buy_user) {
                        if (empty($shop_reward)) {
                            Log::error('订单未生成');
                            return false;
                        }
                        //修改订单状态
                        $this->wxShopRewardUpdate($order['order_id'], UserCode::ORDER_STATUS['已入账待提现'], $order_info, UserCode::REWARD_STATUS['待提现']);

                    }
                }
                break;
            case 'channels_ec_aftersale_update':
                //售后单更新通知

                if ($order['status'] == WxShopCode::ORDER_STATUS['全部商品售后之后订单取消']) {
                    //查询unionid
                    $buy_user = UserService::get_user_by_unionid($order['unionid']);
                    if ($buy_user) {
                        if (empty($shop_reward)) {
                            Log::error('订单未生成');
                            return false;
                        }
                        //修改订单状态
                        $this->wxShopRewardUpdate($order['order_id'], UserCode::ORDER_STATUS['售后已退款'], $order_info, UserCode::REWARD_STATUS['订单关闭']);
                    }
                }

                break;
            case 'channels_ec_guarantee_update':
                //保障单更新通知
                break;
        }


        return true;

    }

    //查询订单详情
    public function getWxShopOrder($order_id)
    {

        $res = wx_shop::get_order($order_id);

        if ($res['errcode'] === 0) {
            $order = $res['order'];
            $data = [
                'unionid' => $order['unionid'],
                'openid' => $order['openid'],
                'order_id' => $order['order_id'],
                'status' => $order['status'],
                'order_info' => json_encode($order, JSON_UNESCAPED_UNICODE),
            ];
        } else {
            Log::error('微信小店订单查询失败：' . $res['errmsg']);
            //抛异常
            return null;
        }

        return $data;
    }


}